package org.fengdi.store.controller;

import org.apache.log4j.Logger;
import org.fengdi.store.entity.Storage;
import org.fengdi.store.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/storage")
public class StorageController {
    @Autowired
    private StorageService storageService;

    // 根据商品码获取库存
    @GetMapping
    public Storage getStorage(@RequestParam("commodity_code") String code) {
        return this.storageService.findStorageByCommodityCode(code);
    }

    // 根据商品码减少库存
    @PostMapping
    public boolean decreaseStorage(@RequestParam("commodity_code") String code, @RequestParam("count") Integer count) {
        try {
            this.storageService.decreaseStorage(code, count);
        }catch (RuntimeException e){
            Logger logger = Logger.getLogger(StorageController.class);
            logger.info(e.getMessage(), e);
            return false;
        }
        return true;
    }
}
